//
//  IndexScrollView.mm
//  AudiHistory
//
//  Created by niko on 11-8-21.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "IndexScrollView.h"
#import "CPageDataStruct.h"
#import "AudiHistoryViewController.h"

@implementation IndexBtn
@synthesize nPage;

@end

@implementation IndexScrollView

- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:CGRectMake(frame.origin.x + 15,frame.origin.y, frame.size.width - 30, frame.size.height - 5)])) {
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = YES;
		self.indicatorStyle = UIScrollViewIndicatorStyleWhite;
        //self.bouncesZoom = YES;
        //self.decelerationRate = UIScrollViewDecelerationRateFast;
        self.delegate = self;
    }
    return self;
}

-(void)GoPage:(id)obj
{
	IndexBtn *btnBtnL = (IndexBtn*)obj;

	[[AudiHistoryViewController SharedObj] SetCurNewPage:btnBtnL.nPage];
}

-(void)Build
{
	int nPageCount = [[CPageDataStructTable SharedObj] GetPageCount];
	
	int nTotalWidth = 0;
	int nTotalHeight = 0;
	
	for (int i = 0; i < nPageCount; i++)
	{
		CPageDataStruct *pPageDataStruct = [CPageDataStructTable GetPageDataByPagenum:i];
		
		if (nil == pPageDataStruct)
		{
			return;
		}
		
		
		IndexBtn *btnBtnL = [IndexBtn buttonWithType:UIButtonTypeCustom];
		
		UIImage *imageL = [[UIImage alloc] initWithContentsOfFile:fullPathFromRelativePath(pPageDataStruct.strImageThumb)];
		[btnBtnL setImage:imageL forState:UIControlStateNormal];
		btnBtnL.backgroundColor = [UIColor clearColor];
		
		btnBtnL.nPage = i;
		
		int btnWidth = imageL.size.width + 10;
		btnBtnL.frame = CGRectMake(i * btnWidth, self.center.y - (imageL.size.height * 0.5), imageL.size.width, imageL.size.height);
		[btnBtnL addTarget:self action:@selector(GoPage:) forControlEvents:UIControlEventTouchUpInside];
		[self addSubview:btnBtnL];
		RELEASE_OBJECT(imageL);
		
		nTotalWidth += btnWidth;
		nTotalHeight = imageL.size.height;
	}
	
	self.contentSize = CGSizeMake(nTotalWidth, self.contentSize.height);
}

-(void)Clear
{	
	[m_arrayBtn removeAllObjects];
	RELEASE_OBJECT(m_arrayBtn);
}

-(void)dealloc
{
	[self Clear];
	
	[super dealloc];
}

@end


@implementation IndexView

-(void)Build
{
	m_pBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
	[self addSubview:m_pBackView];
	m_pBackView.backgroundColor = [UIColor blackColor];
	m_pBackView.alpha = 0.6;
	
	m_pIndexScrollView = [[IndexScrollView alloc]  initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
	[self addSubview:m_pIndexScrollView];
	[m_pIndexScrollView Build];
}

-(void)Clear
{	
	[m_pIndexScrollView removeFromSuperview];
	RELEASE_OBJECT(m_pIndexScrollView);
	
	[m_pBackView removeFromSuperview];
	RELEASE_OBJECT(m_pBackView);
}

-(void)dealloc
{
	[self Clear];
	
	[super dealloc];
}

@end
